﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ArmoryBrowser.Entities;
using System.IO;
using System.Xml.Linq;
using System.Windows.Navigation;
using achievements = ArmoryBrowser.Entities.Achievements;
using acDetails = ArmoryBrowser.Entities.AchievementDetails;

namespace ArmoryBrowser.Controllers
{
    public class ArmoryController
    {
        private Uri serverUri = new Uri("http://Armory.wow-europe.com");

        private static ArmoryController _instance = new ArmoryController();

        private ArmoryController(){}

        public static ArmoryController Instance
        {
            get
            {
                return _instance;
            }
        }

        public void GetCharacterPageAsync(String realmName, String characterName, Action<CharacterPage> dlg)
        {
            UriBuilder builder = new UriBuilder(serverUri);
            builder.Path = "character-sheet.xml";
            // TODO: Quitar los nombres
            builder.Query = String.Format("r={0}&cn={1}", realmName, characterName);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(builder.Uri);
            req.Headers["User-Agent"] = "MSIE 7.0";
            req.BeginGetResponse(delegate(IAsyncResult result) {
                WebResponse resp = req.EndGetResponse(result);
                using (Stream str = resp.GetResponseStream())
                {
                    XDocument doc = XDocument.Load(str);
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    dlg(new CharacterPage(doc.Root)));
                }
            }, null);
        }

        public void GetAchievementsSummaryAsync(String realmName, String characterName, Action<achievements.AchievementsPage> dlg)
        {
            UriBuilder builder = new UriBuilder(serverUri);
            builder.Path = "character-achievements.xml";
            // TODO: Quitar los nombres
            builder.Query = String.Format("r={0}&cn={1}", realmName, characterName);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(builder.Uri);
            req.Headers["User-Agent"] = "MSIE 7.0";
            req.BeginGetResponse(delegate(IAsyncResult result)
            {
                WebResponse resp = req.EndGetResponse(result);
                using (Stream str = resp.GetResponseStream())
                {
                    XDocument doc = XDocument.Load(str);
                    achievements.AchievementsPage page = new achievements.AchievementsPage(doc.Root);
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    dlg(page));
                }
            }, null);
        }

        public void GetAchievementsDetailsAsync(String realmName, String characterName, String category, Action<acDetails.Achievements> dlg)
        {
            UriBuilder builder = new UriBuilder(serverUri);
            builder.Path = "character-achievements.xml";
            // TODO: Quitar los nombres
            builder.Query = String.Format("r={0}&cn={1}&c={2}", realmName, characterName, category);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(builder.Uri);
            req.Headers["User-Agent"] = "MSIE 7.0";
            req.BeginGetResponse(delegate(IAsyncResult result)
            {
                WebResponse resp = req.EndGetResponse(result);
                using (Stream str = resp.GetResponseStream())
                {
                    XDocument doc = XDocument.Load(str);
                    acDetails.Achievements details = new acDetails.Achievements(doc.Root);
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    dlg(details));
                }
            }, null);
        }
    }
}
